package com.luntan.school.dao.impl;

import com.luntan.school.dao.AdminDao;
import com.luntan.school.model.Admin;
import com.luntan.school.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl implements AdminDao {
    @Override
    public Admin getAdminById(int adminId) {
        //获取链接
        Connection connection = DBUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //执行查询并返回结果集
            preparedStatement = connection.prepareStatement("select * from admin where id=?");
            preparedStatement.setInt(1, adminId);
            resultSet = preparedStatement.executeQuery();
            Admin admin = new Admin();
            if (resultSet.next()) {
                admin.setId(resultSet.getInt("id"));
                admin.setAccount(resultSet.getString("account"));
                admin.setPassword(resultSet.getString("password"));
                admin.setName(resultSet.getString("name"));
            }
            return admin;
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放JDBC资源
            DBUtil.release(resultSet, preparedStatement, connection);
        }
        return null;
    }

    @Override
    public Admin login(String account, String password) {
        try {
            //获取链接
            Connection connection = DBUtil.getConnection();


            PreparedStatement preparedStatement = connection.prepareStatement("select * from admin where account=? and password=?");
            preparedStatement.setString(1, account);
            preparedStatement.setString(2, password);
            ResultSet resultSet = preparedStatement.executeQuery();
            Admin admin = new Admin();
            if (resultSet.next()) {
                admin.setId(resultSet.getInt("id"));
                admin.setAccount(resultSet.getString("account"));
//                admin.setPassword(resultSet.getString("password"));不写为防止泄露
                admin.setName(resultSet.getString("name"));

                resultSet.close();
                preparedStatement.close();
                connection.close();
                return admin;
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Admin> getAllAdmins() {
        //获取连接
        Connection connection = DBUtil.getConnection();
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            //执行查询并返回结果集
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select * from admin");
            //遍历结果集
            List<Admin> admins = new ArrayList<>();
            while (resultSet.next()) {
                Admin admin = new Admin();
                admin.setId(resultSet.getInt("id"));
                admin.setAccount(resultSet.getString("account"));
                admin.setName(resultSet.getString("name"));
                admins.add(admin);
            }

            return admins;
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放连接资源
            DBUtil.release(resultSet, statement, connection);
        }
        return null;
    }

    @Override
    public boolean addAdmin(Admin admin) {
        try {
            //获取连接
            Connection connection = DBUtil.getConnection();
            //执行查询并返回结果集
            PreparedStatement preparedStatement = connection.prepareStatement("insert into admin(account,name,password) values(?,?,?)");
            preparedStatement.setString(1, admin.getAccount());
            preparedStatement.setString(3, admin.getPassword());
            preparedStatement.setString(2, admin.getName());
            int result = preparedStatement.executeUpdate();
            //释放连接资源
            preparedStatement.close();
            connection.close();

            return result > 0;  //表示成功建立一行
        }catch (SQLException e) {
            e.printStackTrace();
        }


        return false;
    }

    @Override
    public void updateAdmin(Admin admin) {
        try {
            //获取连接
            Connection connection = DBUtil.getConnection();
            //执行查询并返回结果集
            PreparedStatement preparedStatement = connection.prepareStatement("update admin set password=?,name=?,account=? where id=?");

            preparedStatement.setString(1, admin.getPassword());
            preparedStatement.setString(2, admin.getName());
            preparedStatement.setString(3, admin.getAccount());
            preparedStatement.setInt(4, admin.getId());
            preparedStatement.executeUpdate();

            preparedStatement.close();
            connection.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteAdmin(int adminId) {
        try {
            //获取连接获取连接
            Connection connection = DBUtil.getConnection();
            //执行查询并返回结果集
            PreparedStatement preparedStatement = connection.prepareStatement("delete from admin where id=?");

            preparedStatement.setInt(1, adminId);
            preparedStatement.executeUpdate();

            preparedStatement.close();
            connection.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        AdminDao adminDao = new AdminDaoImpl();
        System.out.println(adminDao.getAdminById(1102));
        System.out.println(adminDao.login("admin","123456"));
    }
}
